/*
Program Name
Copyright (C) 2008 Matthew Perry

This library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This libary/program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using BaseTools.Configuration;
using BaseTools.XS;
using SourceTools.UI;
using BaseTools.Helpers;

namespace SourceTools.IO
{
	/// <summary>
	/// SourceTools XSettings for storing game paths
	/// </summary>
	public class SourceSettings
	{
		//private static XSettings sourceSettings = new XSettings("SourceTools");
		
		/// <summary>
		/// Gamepath id for Counter-Strike: Source
		/// </summary>
		public const string GAMEPATH_CSTRIKE = "CSTRIKE";
		
		/// <summary>
		/// Gamepath id for Garry's Mod
		/// </summary>
		public const string GAMEPATH_GMOD = "GMOD";
		
		/// <summary>
		/// Gamepath id for Half-Life 2
		/// </summary>
		public const string GAMEPATH_HL2 = "HL2";
		
		/// <summary>
		/// Gamepath id for Half-Life 2: Episode One
		/// </summary>
		public const string GAMEPATH_HL2EP1 = "HL2EP1";
		
		/// <summary>
		/// Gamepath id for Half-Life 2: Episode Two
		/// </summary>
		public const string GAMEPATH_HL2EP2 = "HL2EP2";
		
		/// <summary>
		/// Gamepath id for Day of Defeat: Source
		/// </summary>
		public const string GAMEPATH_DOD = "DOD";
		
		/// <summary>
		/// Gamepath id for Team Fortress 2
		/// </summary>
		public const string GAMEPATH_TF2 = "TF2";
		
		/// <summary>
		/// Filename for game paths
		/// </summary>
		public const string FILE_GAMEPATH = "gamepath.xbt";
		
		/// <summary>
		/// Opens the game path configuration gui
		/// </summary>
		public static void ConfigureGamePaths()
		{
			UIGamePath gp = new UIGamePath();
			gp.FillFields();
			gp.ShowDialog();
		}
		
		/// <summary>
		/// Game paths for SourceTools
		/// </summary>
		public static Dictionary<string, string> GamePaths
		{
			get
			{
				try
				{
					//sourceSettings.EnsureProgram();
					XConf.EnsureXConf();
					//return GenericConverter.ReadSection(
					//	new SourceConfig().LoadConfiguration(sourceSettings.RelativeTo(FILE_GAMEPATH)));
					return GenericConverter.ReadSection(XConf.LoadConfig(FILE_GAMEPATH));
				}
				catch(Exception)
				{
					return new Dictionary<string, string>();
				}
			}
			set
			{
				//sourceSettings.EnsureProgram();
				//new SourceConfig().SaveConfiguration(sourceSettings.RelativeTo(FILE_GAMEPATH),
				//                                     GenericConverter.WriteSection(value));
				XConf.EnsureXConf();
				XConf.SaveConfig(FILE_GAMEPATH, GenericConverter.WriteSection(value));
			}
		}
		
		/// <summary>
		/// Individual game path setting
		/// </summary>
		public static GamePathIndexer GamePath
		{
			get
			{
				return new GamePathIndexer();
			}
		}
		
		/// <summary>
		/// Game path indexer
		/// </summary>
		public class GamePathIndexer
		{
			/// <summary>
			/// Game path setting
			/// </summary>
			public string this[string gameid]
			{
				get
				{
					Dictionary<string, string> dict = GamePaths;
					
					if (dict.ContainsKey(gameid))
						return dict[gameid];
					else
						return null;
				}
				set
				{
					Dictionary<string, string> dict = GamePaths;
					
					if (dict.ContainsKey(gameid))
						dict[gameid] = value;
					else
						dict.Add(gameid, value);
					
					GamePaths = dict;
				}
			}
		}
	}
}
